package cn.sdormitory.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.controller.forms.FilterStudentQueryForm;
import cn.sdormitory.controller.vos.FilterStudentVo;
import cn.sdormitory.leavingSchool.vo.LeavingSchoolCertificateVO;
import cn.sdormitory.workflow.entity.StuLeaveApply;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.dto.ResultMsg;
import cn.sdormitory.service.AppSdStudentLeaveService;
import cn.sdormitory.service.AppStudentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @创建人：zhouyang
 * @创建时间：2021/1/6 14:59
 * @version：V1.0
 */
@RestController
@RequestMapping("/app/Student")
@Api("学员操作接口")
public class AppStudentController {
    @Autowired
    private AppStudentService appStudentService;

    @Autowired
    private AppSdStudentLeaveService appSdStudentLeaveService;

    @ApiOperation(value = "获取学生过滤分页")
    @GetMapping("/getFilterStudentPage")
    public CommonResult<CommonPage<FilterStudentVo>> getFilterStudentPage(FilterStudentQueryForm form){
        Page<FilterStudentVo> page = appStudentService.getFilterStudentPage(form);
        return CommonResult.success(CommonPage.restPage(page));
    }

    //根据ID获取学生详情
    @ApiOperation(value = "学生详细信息")
    @GetMapping("/getStudentById/{id}")
    public ResultMsg getStudentById(@PathVariable("id") int id){
        BStudent bStudent = appStudentService.getStudentById(id);
        if(bStudent != null){
            return ResultMsg.BY_SUCCESS("查询学生信息成功!!!", bStudent);
        }else{
            return ResultMsg.BY_FAIL("查询学生信息失败!!!");
        }
    }
    
    //批量获取学生详情
    @ApiOperation(value = "学生详细信息")
    @PostMapping("/getStudentByIds")
    public ResultMsg getStudentByIds(@RequestBody Map<String, Object> objectMap){
        List<String> studentNos = (List<String>)objectMap.get("studentNos");
        List<BStudent> bStudents = appStudentService.getStudentsByStudentNos(studentNos);;
        if(CollectionUtil.isNotEmpty(bStudents)){
            return ResultMsg.BY_SUCCESS("查询学生信息成功!!!", bStudents);
        }else{
            return ResultMsg.BY_FAIL("查询学生信息失败!!!");
        }
    }

    @ApiOperation(value = "根据宿舍号查询学生列表")
    @GetMapping("/getStudentByDormitoryId/{dormitoryId}")
    public CommonResult getStudentByDormitoryId(@PathVariable("dormitoryId") Long dormitoryId){
        List<BStudent> list = appStudentService.getStudentByDormitoryId(dormitoryId);
        if(list != null && list.size() > 0){
            return CommonResult.success(list);
        }else{
            return CommonResult.success("当前宿舍无学生!!!");
        }
    }

    @ApiOperation(value = "resetPwd => 修改密码", notes = "重置密码")
    @PutMapping(value = "/resetPwd")
    public CommonResult<Integer> resetPwd(@RequestBody Map<String,String> map) {
        // 更新密码
        int result = appStudentService.resetPwd(map);
        if (result > 0) {
            return CommonResult.success(result);
        }else if( result == -1){
            return CommonResult.failed("旧密码错误!!!");
        }else if(result == -2){
            return CommonResult.failed("新密码两次输入不一致!!!");
        }
        return CommonResult.failed();

    }

    @PostMapping("/createLeaveApply")
    @ApiOperation(value = "学生请假")
    public CommonResult createLeaveApply(@RequestBody StuLeaveApply leaveApply){
        int num = appSdStudentLeaveService.createLeaveApply(leaveApply);
        if(num > 0){
            return CommonResult.success();
        }else if( num == -1){
            return CommonResult.failed("不允许重复请假");
        }else if (num == -2){
            return CommonResult.failed("当前请假开始时间已过考勤时间，请重新选择请假开始时间！");
        }else if (num == -3) {
            return CommonResult.failed("宿舍卫生不合格，无法请假!");
        } else {
            return CommonResult.failed("请假失败");
        }
    }
    
    @ApiOperation("studentCancelLeave => 学生销假申请")
    @PostMapping("/studentCancelLeave")
    public CommonResult studentCancelLeaveApply(@RequestBody StuLeaveApply leaveApply) {
        int count = appSdStudentLeaveService.studentCancelLeaveApply(leaveApply);
        return CommonResult.success(count);
    }
    
    @PostMapping("/createNewLeaveApply")
    @ApiOperation(value = "学生请假")
    public CommonResult createNewLeaveApply(@RequestBody StuLeaveApply leaveApply){
        int num = appSdStudentLeaveService.createNewLeaveApply(leaveApply);
        if(num > 0){
            return CommonResult.success();
        }else if( num == -1){
            return CommonResult.failed("请假时间有重复,不允许重复请假");
        }else if (num == -2){
            return CommonResult.failed("当前请假开始时间已过考勤时间，请重新选择请假开始时间！");
        }else if (num == -3) {
            return CommonResult.failed("宿舍卫生不合格，无法请假!");
        } else {
            return CommonResult.failed("请假失败");
        }
    }

    @GetMapping("/getStuLeaveApplyList/{studentId}")
    @ApiOperation(value = "学生请假列表")
    public CommonResult getStuLeaveApplyList(@PathVariable Long studentId){
        List<StuLeaveApply> list = appSdStudentLeaveService.getStuLeaveApplyList(studentId);
        return CommonResult.success(list);
    }

    @GetMapping("/revokeStuLeaveApply/{stuLeaveApplyId}")
    @ApiOperation(value = "学生请假列表")
    public CommonResult revokeStuLeaveApply(@PathVariable("stuLeaveApplyId") Long stuLeaveApplyId){
        int num = appSdStudentLeaveService.revokeStuLeaveApply(stuLeaveApplyId);
        if(num > 0){
            return CommonResult.success("撤回成功");
        }else if(num == -1){
            return CommonResult.failed("不可撤回");
        }else {
            return CommonResult.failed();
        }
    }


    /**
     * 查询请假学生相关信息
     * @param studentId
     * @return
     */
    @GetMapping("/studentLeaveCorrelationInfo/{studentId}")
    public CommonResult studentLeaveCorrelationInfo(@PathVariable Long studentId){

        List<StuLeaveApply> stuLeaveApply = appSdStudentLeaveService.studentLeaveCorrelationInfo(studentId);

        if(stuLeaveApply == null){
            return CommonResult.failed("查询失败！！！");
        }
        System.out.println(stuLeaveApply);

        return CommonResult.success(stuLeaveApply);
    }

    @ApiOperation(value = "获取离校凭证信息")
    @GetMapping("/getLeavingSchoolCertificate")
    public CommonResult<LeavingSchoolCertificateVO> getLeavingSchoolCertificate(Long studentId){
        LeavingSchoolCertificateVO schoolCertificateVO = appStudentService.getLeavingSchoolCertificate(studentId);
        return CommonResult.success(schoolCertificateVO);
    }

}
